﻿using System;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using YogaStudio.Data.Domain;
using YogaStudio.Service.BLL;

namespace YogaStudio.WebUI.Controllers
{
    [Authorize(Roles = "Admin")]
    public class ProductAdminController : Controller
    {
        private readonly IProductService _productService;
        private readonly ICategoryService _categoryService;

        public ProductAdminController() : this(new ProductService(), new CategoryService()) { }

        public ProductAdminController(IProductService productService, ICategoryService categoryService)
        {
            _productService = productService;
            _categoryService = categoryService;
        }

        public ActionResult Index()
        {
            var products = _productService.GetAllProducts();
            return View(products.ToList());
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = _productService.GetProductyId(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(_categoryService.GetAllCategories(), "CategoryId", "CategoryName");
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ProductId,CategoryId,ProductName,Description,SearchAttributes,ImageUrl,Price,IsAvailable")] Product product, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Content/Images/" + file.FileName));
                    product.ImageUrl = "~/Content/Images/" + file.FileName;
                }
                _productService.AddProduct(product);
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(_categoryService.GetAllCategories(), "CategoryId", "CategoryName", product.CategoryId);
            return View(product);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = _productService.GetProductyId(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryId = new SelectList(_categoryService.GetAllCategories(), "CategoryId", "CategoryName", product.CategoryId);
            return View(product);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ProductId,CategoryId,ProductName,Description,SearchAttributes,ImageUrl,Price,IsAvailable")] Product product, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Content/Images/" + file.FileName));
                    product.ImageUrl = "~/Content/Images/" + file.FileName;
                }
                _productService.UpdateProduct(product);
                return RedirectToAction("Index");
            }
            ViewBag.CategoryId = new SelectList(_categoryService.GetAllCategories(), "CategoryId", "CategoryName", product.CategoryId);
            return View(product);
        }

        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = _productService.GetProductyId(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
                Product product = _productService.GetProductyId(id);
                if (CheckProductToDelte(product))
                {
                    _productService.DeleteProduct(product);
                    return RedirectToAction("Index");
                }
            TempData["Message"] = "Cannot delete product. The product is in the shopping cart or in Order Items";
            return RedirectToAction("Delete", new { id });
        }

        private bool CheckProductToDelte(Product product)
        {
            if (product.ShoppingCarts.Count > 0 || product.OrderItems.Count > 0)
            {
                return false;
            }
            return true;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _productService.Dispose(true);
                _categoryService.Dispose(true);
            }
            base.Dispose(disposing);
        }
    }
}
